<?php
//除架构文件外，所有类的父类
abstract class Zen_Base
{
    protected $_config;

    protected $_Zen_Base = array();

    public function __construct($config = null)
    {
        $this->_preConfig();
        // build configuration
        $this->_config = array_merge(
            $this->_buildConfig(get_class($this)),
            (array) $config
        );

        // post-configuration tasks
        $this->_postConfig();

        // post-construction tasks
        $this->_postConstruct();
    }

    public function __destruct()
    {
    }

    protected function _buildConfig($class)
    {
        if (! $class) {
            return array();
        }

        $config = isset(Zen::$config[$class])?Zen::$config[$class]:array();

        $var    = "_$class";
        $prop  =  empty($this->$var)
                ? array()
                : (array) $this->$var;

        $parent = get_parent_class($class);
        return array_merge($this->_buildConfig($parent), $prop, $config);

    }

    protected function _exception($code, $info = array())
    {
        static $class;
        if (! $class) {
            $class = get_class($this);
        }

        return Zen::exception(
            $class,
            $code,
            $this->locale($code, 1, $info),
            (array) $info
        );
    }

    public function locale($key, $num = 1, $replace = null)
    {
        static $class;
        if (! $class) {
            $class = get_class($this);
        }

        static $locale;
        if (! $locale) {
            $locale = Zen_Registry::get('locale');
        }

        return $locale->fetch($class, $key, $num, $replace);
    }

    //Hook
    protected function _preConfig()
    {
    }

    //Hook
    protected function _postConfig()
    {
    }

    //Hook
    protected function _postConstruct()
    {
    }
}